package com.rjpacket.supernews.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.rjpacket.supernews.app.R;
import com.rjpacket.supernews.app.models.News;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WIN7 on 2015/3/12.
 */
public class FunnyNewsAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context context;
    private List<News> data;
    private ViewHolder holder;

    public FunnyNewsAdapter(Context context, List<News> data) {

        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        int ret = 0;
        if (data != null) {
            ret = data.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (data != null) {
            ret = data.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        News news = data.get(position);
        ArrayList<String> thumbs = news.getThumbs();
        int size = thumbs.size();
        switch (size) {
            case 0:
                convertView = inflater.inflate(R.layout.rjp_item_news_0, parent, false);
                holder = new ViewHolder(convertView);
                break;
            case 1:
                convertView = inflater.inflate(R.layout.rjp_item_news_1, parent, false);
                holder = new ViewHolder(convertView);
                break;
            case 3:
                convertView = inflater.inflate(R.layout.rjp_item_news_3, parent, false);
                holder = new ViewHolder(convertView);
                break;
        }
        convertView.setTag(holder);

        String title = news.getTitle();
        holder.title.setText(title);
        holder.zan.setText(news.getComment_num() + "");
        holder.source.setText(news.getSource());
        return convertView;
    }

    private class ViewHolder {
        public TextView title;
        public TextView zan;
        public TextView source;

        public ViewHolder(View itemView) {
            title = (TextView) itemView.findViewById(R.id.news_title);
            zan = (TextView) itemView.findViewById(R.id.news_zan);
            source = (TextView) itemView.findViewById(R.id.news_source);
        }
    }
}
